﻿using System;
using Jonathan.Data;
using Microsoft.Win32;

namespace Jonathan.DataAccess
{
   /// <summary>
   /// RegistryData for jonathan.
   /// </summary>
   public static class RegistryData
   {
      /// <summary>
      /// Gets if the user wants to save data per user or not.
      /// </summary>
      /// <returns>True if the user wants to save data per user, false if per machine.</returns>
      public static bool GetSavePerUser ()
      {
         return GetLocalMachineValue (Common.REGISTRY_PATH, "SaveDataPerUser").ToBool ();
      }
      
      /// <summary>
      /// Gets a string value from the Local Machine branch in the registry.
      /// </summary>
      /// <param name="subkeyPath">Path to Sub Key.</param>
      /// <param name="itemName">Name of Item to get.</param>
      /// <returns>Value requested, or empty string.</returns>
      public static string GetLocalMachineValue (string subkeyPath, string itemName)
      {
         return GetValue (Registry.LocalMachine, subkeyPath, itemName);
      }
      
      /// <summary>
      /// Gets a string value from the registry.
      /// </summary>
      /// <param name="baseKey">Base Key to start.</param>
      /// <param name="subkeyPath">Path to Sub Key.</param>
      /// <param name="itemName">Name of Item to get.</param>
      /// <returns>Value requested, or empty string.</returns>
      public static string GetValue (RegistryKey baseKey, string subkeyPath, string itemName)
      {
         string value = string.Empty;
         RegistryKey subKey = baseKey.OpenSubKey (subkeyPath);
         if (subKey != null)
         {
            try
            {
               value = subKey.GetValue (itemName).ToString ();
            }
            catch (Exception ex)
            {
               value = ex.Message;
            }
            subKey.Close ();
            subKey.Dispose ();
         }

         return value;
      }
   }
}
